package com.wzdigit.wms.hm.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.wzdigit.framework.config.SwaggerTemplate;
import com.wzdigit.framework.model.swagger.SwaggerApiInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.LinkedList;
import java.util.List;

/**
 * @author hsiang
 */
@EnableSwagger2
//@EnableSwaggerBootstrapUI
@EnableKnife4j
@Configuration
public class SwaggerConfiguration extends SwaggerTemplate {
	
	@Value("${swagger.enable:true}")
	private boolean enableSwagger;
	
	@Value("${swagger.title:}")
	private String swaggerTitle;
	
	@Value("${swagger.version:}")
	private String swaggerVersion;
	
	@Override
	protected boolean enableSwagger() {
		return enableSwagger;
	}
	
    @Bean
    public SwaggerApiInfo info() {
		return SwaggerApiInfo.builder().title(swaggerTitle).version(swaggerVersion).serviceUrl(null)
				.statusList(extractStatusCodes()).build();
    }

    private List<ResponseMessage> extractStatusCodes() {
        final LinkedList<ResponseMessage> list = new LinkedList<>();
        for (StatusCode statusCodes : StatusCode.values()) {
            final ResponseMessageBuilder builder = new ResponseMessageBuilder();
            final ResponseMessage message = builder
                    .code(Integer.valueOf(statusCodes.code()))
                    .message(statusCodes.message())
                    .build();
            list.add(message);
        }
        return list;
    }

}
